﻿using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;

public class TextureImportSettings : ScriptableObject {
	
	[MenuItem("Tools/Texture/Change Texture Format/AutoCompressed")]
	static void ChangeTextureFormat_AutoCompressed()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.AutomaticCompressed);
	}
	
	[MenuItem("Tools/Texture/Change Texture Format/Auto 16Bit")]
	static void ChangeTextureFormat_Auto16Bit()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.Automatic16bit);
	}
	
	[MenuItem("Tools/Texture/Change Texture Format/Auto Truecolor")]
	static void ChangeTextureFormat_AutoTruecolor()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.AutomaticTruecolor);
	}
	
	[MenuItem("Tools/Texture/Change Texture Format/RGB Compressed DXT1")]
	static void ChangeTextureFormat_RGB_XT1()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT1);
	}
	
	[MenuItem("Tools/Texture/Change Texture Format/RGB Compressed DXT5")]
	static void ChangeTextureFormat_RGB_XT5()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT5);
	}
	
	[MenuItem("Tools/Texture/Change Texture Format/RGB 16 Bit")]
	static void ChangeTextureFormat_RGB_16Bit()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB16);
	}
	
	[MenuItem("Tools/Texture/Change Texture Format/RGB 24 Bit")]
	static void ChangeTextureFormat_RGB_24Bit()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB24);
	}
	
	[MenuItem("Tools/Texture/Change Texture Format/Alpha 8 Bit")]
	static void ChangeTextureFormat_Alpha_8Bit()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.Alpha8);
	}
	
	[MenuItem("Tools/Texture/Change Texture Format/RGBA 16 Bit")]
	static void ChangeTextureFormat_RGBA_16Bit()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB16);
	}
	
	[MenuItem("Tools/Texture/Change Texture Format/RGBA 32 Bit")]
	static void ChangeTextureFormat_RGBA_32Bit()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.RGBA32);
	}
	
	[MenuItem("Tools/Texture/Change Texture Format/ARGB 32 Bit")]
	static void ChangeTextureFormat_ARGB_32Bit()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB32);
	}
	
	[MenuItem("Tools/Texture/Change Texture Format/RGB PVRTC 2Bit")]
	static void ChangeTextureFormat_RGB_PVRTC_2Bit()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGB2);
	}
	
	[MenuItem("Tools/Texture/Change Texture Format/RGB PVRTC 4Bit")]
	static void ChangeTextureFormat_RGB_PVRTC_4Bit()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGB4);
	}
	
	[MenuItem("Tools/Texture/Change Texture Format/RGBA PVRTC 4Bit")]
	static void ChangeTextureFormat_RGBA_PVRTC_4Bit()
	{
		SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGBA4);
	}
	
	//-------------------------------------------------------------------
	[MenuItem("Tools/Texture/Change Texture Size/Change Max Texture Size/32")]
	static void ChangeTextureSize_32()
	{
		SelectedChangeMaxTextureSize(32);
	}
	
	[MenuItem("Tools/Texture/Change Texture Size/Change Max Texture Size/64")]
	static void ChangeTextureSize_64()
	{
		SelectedChangeMaxTextureSize(64);
	}
	
	[MenuItem("Tools/Texture/Change Texture Size/Change Max Texture Size/128")]
	static void ChangeTextureSize_128()
	{
		SelectedChangeMaxTextureSize(128);
	}
	
	[MenuItem("Tools/Texture/Change Texture Size/Change Max Texture Size/256")]
	static void ChangeTextureSize_256()
	{
		SelectedChangeMaxTextureSize(256);
	}
	
	[MenuItem("Tools/Texture/Change Texture Size/Change Max Texture Size/512")]
	static void ChangeTextureSize_512()
	{
		SelectedChangeMaxTextureSize(512);
	}
	
	[MenuItem("Tools/Texture/Change Texture Size/Change Max Texture Size/1024")]
	static void ChangeTextureSize_1024()
	{
		SelectedChangeMaxTextureSize(1024);
	}
	
	[MenuItem("Tools/Texture/Change Texture Size/Change Max Texture Size/2048")]
	static void ChangeTextureSize_2048()
	{
		SelectedChangeMaxTextureSize(2048);
	}
	
	//-----------------------------------------------------------------------------
	[MenuItem("Tools/Texture/Change MipMap/Enable MipMap")]
	static void ChangeMipMap_On()
	{
		SelectedChangeMipMap(true);
	}
	
	[MenuItem("Tools/Texture/Change MipMap/Disable MipMap")]
	static void ChangeMipMap_Off()
	{
		SelectedChangeMipMap(false);
	}
	
	//----------------------------------------------------------------------------
	[MenuItem("Tools/Texture/Change No Power of 2/None")]
	static void ChangeNPOT_None()
	{
		SelectedChangeNonPowerOf2(TextureImporterNPOTScale.None);
	}
	
	[MenuItem("Tools/Texture/Change No Power of 2/ToNearest")]
	static void ChangeNPOT_ToNearest()
	{
		SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToNearest);
	}
	
	[MenuItem("Tools/Texture/Change No Power of 2/ToLarger")]
	static void ChangeNPOT_ToLarger()
	{
		SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToLarger);
	}
	
	[MenuItem("Tools/Texture/Change No Power of 2/ToSmaller")]
	static void ChangeNPOT_ToSmaller()
	{
		SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToSmaller);
	}
	//------------------------------------------------------------------------------
	[MenuItem("Tools/Texture/Change Is Readable/Enable")]
	static void ChangeIsReadable_Yes()
	{
		SelectedChangeIsReadable(true);
	}
	
	[MenuItem("Tools/Texture/Change Is Readable/Disable")]
	static void ChangeIsReadable_No()
	{
		SelectedChangeIsReadable(false);
	}
	
	[MenuItem("Tools/Texture/Change Type/Advanced")]
	static void ChangeTextureType_Advanced()
	{
		SelectedChangeIsReadable(TextureImporterType.Advanced);
	}
	
	[MenuItem("Tools/Texture/Change Type/Bump")]
	static void ChangeTextureType_Bump()
	{
		SelectedChangeIsReadable(TextureImporterType.Bump);
	}
	
	[MenuItem("Tools/Texture/Change Type/GUI")]
	static void ChangeTextureType_GUI()
	{
		SelectedChangeIsReadable(TextureImporterType.GUI);
	}
	
	[MenuItem("Tools/Texture/Change Type/Image")]
	static void ChangeTextureType_Image()
	{
		SelectedChangeIsReadable(TextureImporterType.Image);
	}
	
	[MenuItem("Tools/Texture/Change Type/Lightmap")]
	static void ChangeTextureType_Lightmap()
	{
		SelectedChangeIsReadable(TextureImporterType.Lightmap);
	}
	
	[MenuItem("Tools/Texture/Change Type/Reflection")]
	static void ChangeTextureType_Reflection()
	{
		SelectedChangeIsReadable(TextureImporterType.Reflection);
	}
	
	//------------------------------------------------------------------
	[MenuItem("Tools/Texture/PerfectPixel UI/All")]
	static void ChangeTexturePerfectPixel_AllUI()
	{
		List<string> uiFolderList = new List<string>();
		uiFolderList.Add("Assets/GameResources/UI/Base/Font");		//基础字库
		uiFolderList.Add("Assets/GameResources/UI/Base/Atlas");		//基础图集
		
		List<Texture> textureList = new List<Texture>();
		foreach(string uiFolder in uiFolderList)
		{
			string[] files = Directory.GetFiles(uiFolder, "*", SearchOption.AllDirectories);
			foreach(string file in files)
			{
				Texture texture = null;
				if(texture == null)
				{
					UIFont font = AssetDatabase.LoadAssetAtPath(file, typeof(UIFont)) as UIFont;
					if(font != null)
						texture = font.texture;
				}
				if(texture == null)
				{
					UIAtlas atlas = AssetDatabase.LoadAssetAtPath(file, typeof(UIAtlas)) as UIAtlas;
					if(atlas != null)
						texture = atlas.texture;
				}
				
				ChangeTexturePerfectPixel_UI(texture);
			}
		}
	}
	
	[MenuItem("Tools/Texture/PerfectPixel UI/Selected")]
	static void ChangeTexturePerfectPixel_SelectedUI()
	{
		Object[] selection = Selection.GetFiltered(typeof(Texture), SelectionMode.DeepAssets);
		foreach(Texture texture in selection)
		{
			ChangeTexturePerfectPixel_UI(texture);
		}
	}
	
	static void ChangeTexturePerfectPixel_UI(Texture texture)
	{
		if(texture == null)
			return;
		
		string filePath = AssetDatabase.GetAssetPath(texture);
		TextureImporter importer = AssetImporter.GetAtPath(filePath) as TextureImporter;
		importer.isReadable = true;
		
		importer.textureType = TextureImporterType.GUI;
		importer.npotScale = TextureImporterNPOTScale.None;
		importer.mipmapEnabled = false;
		importer.filterMode = FilterMode.Point;
		importer.maxTextureSize = 4096;
		importer.textureFormat = TextureImporterFormat.AutomaticTruecolor;
		
		importer.isReadable = false;
		AssetDatabase.ImportAsset(filePath);
	}
	
	//------------------------------------------------------------------
	[MenuItem("Tools/Texture/PerfectPixel And Smooth UI/All")]
	static void ChangeTexturePerfectPixelAndSmooth_AllUI()
	{
		List<string> uiFolderList = new List<string>();
		uiFolderList.Add("Assets/GameResources/UI/Base/Font");		//基础字库
		uiFolderList.Add("Assets/GameResources/UI/Base/Atlas");		//基础图集
		
		List<Texture> textureList = new List<Texture>();
		foreach(string uiFolder in uiFolderList)
		{
			string[] files = Directory.GetFiles(uiFolder, "*", SearchOption.AllDirectories);
			foreach(string file in files)
			{
				Texture texture = null;
				if(texture == null)
				{
					UIFont font = AssetDatabase.LoadAssetAtPath(file, typeof(UIFont)) as UIFont;
					if(font != null)
						texture = font.texture;
				}
				if(texture == null)
				{
					UIAtlas atlas = AssetDatabase.LoadAssetAtPath(file, typeof(UIAtlas)) as UIAtlas;
					if(atlas != null)
						texture = atlas.texture;
				}
				
				ChangeTexturePerfectPixelAndSmooth_UI(texture);
			}
		}
	}
	
	[MenuItem("Tools/Texture/PerfectPixel And Smooth UI/SelectedUI")]
	static void ChangeTexturePerfectPixelAndSmooth_SelectedUI()
	{
		Object[] selection = Selection.GetFiltered(typeof(Texture), SelectionMode.DeepAssets);
		foreach(Texture texture in selection)
		{
			ChangeTexturePerfectPixelAndSmooth_UI(texture);
		}
	}
	
	static void ChangeTexturePerfectPixelAndSmooth_UI(Texture texture)
	{
		if(texture == null)
			return;
		
		string filePath = AssetDatabase.GetAssetPath(texture);
		TextureImporter importer = AssetImporter.GetAtPath(filePath) as TextureImporter;
		importer.isReadable = true;
		
		importer.textureType = TextureImporterType.GUI;
		importer.npotScale = TextureImporterNPOTScale.None;
		importer.mipmapEnabled = false;
		importer.filterMode = FilterMode.Trilinear;
		importer.anisoLevel = 4;
		importer.maxTextureSize = 4096;
		importer.textureFormat = TextureImporterFormat.AutomaticTruecolor;
		
		importer.isReadable = false;
		AssetDatabase.ImportAsset(filePath);
	}
	
	//-----------------------------------------------------------------
	[MenuItem("Tools/Texture/PerfectPixel And ComPressed UI/All")]
	static void ChangeTexturePerfectPixelAndComPressed_AllUI()
	{
		List<string> uiFolderList = new List<string>();
		uiFolderList.Add("Assets/GameResources/UI/Base/Font");		//基础字库
		uiFolderList.Add("Assets/GameResources/UI/Base/Atlas");		//基础图集
		
		List<Texture> textureList = new List<Texture>();
		foreach(string uiFolder in uiFolderList)
		{
			string[] files = Directory.GetFiles(uiFolder, "*", SearchOption.AllDirectories);
			foreach(string file in files)
			{
				Texture texture = null;
				if(texture == null)
				{
					UIFont font = AssetDatabase.LoadAssetAtPath(file, typeof(UIFont)) as UIFont;
					if(font != null)
						texture = font.texture;
				}
				if(texture == null)
				{
					UIAtlas atlas = AssetDatabase.LoadAssetAtPath(file, typeof(UIAtlas)) as UIAtlas;
					if(atlas != null)
						texture = atlas.texture;
				}
				
				ChangeTexturePerfectPixelAndComPressed_UI(texture);
			}
		}
	}
	
	[MenuItem("Tools/Texture/PerfectPixel And ComPressed UI/SelectedUI")]
	static void ChangeTexturePerfectPixelAndComPressed_SelectedUI()
	{
		Object[] selection = Selection.GetFiltered(typeof(Texture), SelectionMode.DeepAssets);
		foreach(Texture texture in selection)
		{
			ChangeTexturePerfectPixelAndComPressed_UI(texture);
		}
	}
	
	static void ChangeTexturePerfectPixelAndComPressed_UI(Texture texture)
	{
		if(texture == null)
			return;
		
		string filePath = AssetDatabase.GetAssetPath(texture);
		TextureImporter importer = AssetImporter.GetAtPath(filePath) as TextureImporter;
		importer.isReadable = true;
		
		importer.textureType = TextureImporterType.GUI;
		importer.npotScale = TextureImporterNPOTScale.None;
		importer.mipmapEnabled = false;
		importer.filterMode = FilterMode.Trilinear;
		importer.anisoLevel = 4;
		importer.maxTextureSize = 4096;
		importer.textureFormat = TextureImporterFormat.Automatic16bit;
		
		importer.isReadable = false;
		AssetDatabase.ImportAsset(filePath);
	}
	
	//-----------------------------------------------------------
	static void SelectedChangeIsReadable(TextureImporterType type)
	{
		Object[] textures = GetSelectedTextures();
		Selection.objects = new Object[0];
		foreach(Texture2D texture in textures)
		{
			string path = AssetDatabase.GetAssetPath(texture);
			TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
			textureImporter.textureType = type;
			AssetDatabase.ImportAsset(path);
		}
	}
	
	static void SelectedChangeIsReadable(bool enabled)
	{
		Object[] textures = GetSelectedTextures();
		Selection.objects = new Object[0];
		foreach(Texture2D texture in textures)
		{
			string path = AssetDatabase.GetAssetPath(texture);
			TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
			textureImporter.isReadable = enabled;
			AssetDatabase.ImportAsset(path);
		}
	}
	
	static void SelectedChangeNonPowerOf2(TextureImporterNPOTScale npot)
	{
		Object[] textures = GetSelectedTextures();
		Selection.objects = new Object[0];
		foreach(Texture2D texture in textures)
		{
			string path = AssetDatabase.GetAssetPath(texture);
			TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
			textureImporter.npotScale = npot;
			AssetDatabase.ImportAsset(path);
		}
	}
	
	static void SelectedChangeMipMap(bool enabled)
	{
		Object[] textures = GetSelectedTextures();
		Selection.objects = new Object[0];
		foreach(Texture2D texture in textures)
		{
			string path = AssetDatabase.GetAssetPath(texture);
			TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
			textureImporter.mipmapEnabled = enabled;
			AssetDatabase.ImportAsset(path);
		}
	}
	
	static void SelectedChangeMaxTextureSize(int size)
	{
		Object[] textures = GetSelectedTextures();
		Selection.objects = new Object[0];
		foreach(Texture2D texture in textures)
		{
			string path = AssetDatabase.GetAssetPath(texture);
			TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
			textureImporter.maxTextureSize = size;
			AssetDatabase.ImportAsset(path);
		}
	}
	
	static void SelectedChangeTextureFormatSettings(TextureImporterFormat newFromat)
	{
		Object[] textures = GetSelectedTextures();
		Selection.objects = new Object[0];
		foreach(Texture2D texture in textures)
		{
			string path = AssetDatabase.GetAssetPath(texture);
			TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
			textureImporter.textureFormat = newFromat;
			AssetDatabase.ImportAsset(path);
		}
	}
	
	static Object[] GetSelectedTextures()
	{
		return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
	}
}

